package com.example.goodtrash.search;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.goodtrash.R;
import com.example.goodtrash.db.GoodTrashDB;
import com.example.goodtrash.model.History;
import com.google.android.material.appbar.MaterialToolbar;

import java.util.ArrayList;
import java.util.List;

public class searchHistory extends AppCompatActivity {
    private List<History> dataList=new ArrayList<>();
    private HistoryAdapter adapter;
    private RecyclerView recyclerView;
    private MaterialToolbar toolbar;
    private GoodTrashDB db;
    private LinearLayout empty;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_history);
        empty= findViewById(R.id.lay_empty_data);
        recyclerView=findViewById(R.id.rv_history);
        toolbar=findViewById(R.id.toolbar_history);
        db=GoodTrashDB.getInstance(searchHistory.this);
        adapter=new HistoryAdapter(dataList,searchHistory.this,searchHistory.this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        Button button=findViewById(R.id.bt_remove_all);
        showHistoryData();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<History> list=db.loadHistory();
                for (History history:list){
                    db.removeHistory(history);
                }
                showHistoryData();
            }
        });

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void showHistoryData(){
        List<History> historyList=db.loadHistory();
        if (historyList.size()>0){
            dataList.clear();
            for (History history:historyList){
                dataList.add(history);
            }
            adapter.notifyDataSetChanged();
            empty.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }else {
            empty.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }
    }

}